clc; clear all;

%% Metric FLAGS
do_niqe = 0;
do_brisque = 0;
do_divine = 1;
do_nrpqa = 0;




%% initialize metric stuff

% niqe

if do_niqe
    load niqe_modelparameters.mat
    blocksizerow    = 96;
    blocksizecol    = 96;
    blockrowoverlap = 0;
    blockcoloverlap = 0;
end

if do_divine
    %load divine_data_live_trained.mat
    
    % initialize dependend libraries 
    addpath('C:\libsvm-311\matlab'); % here is not the current version used, since version 3.18 doesn't work with the provided training data
    addpath('C:\Program Files (x86)\MATLAB\R2013a Student\toolbox\matlabPyrTools');
end


%%

path_img_orig = 'UCID_UK_Database/original/';
path_img_singlecompr = 'UCID_UK_Database/single_compression/';
path_img_doublecompr = 'UCID_UK_Database/double_compression/';

dir_img_orig = dir(path_img_orig);
dir_img_singlecompr = dir(path_img_singlecompr);
dir_img_doublecompr = dir(path_img_doublecompr);
    
%remove '..' and '.' directories
dir_img_orig(strcmp({dir_img_orig.name},'..'))=[];
dir_img_orig(strcmp({dir_img_orig.name},'.'))=[];
dir_img_singlecompr(strcmp({dir_img_singlecompr.name},'..'))=[];
dir_img_singlecompr(strcmp({dir_img_singlecompr.name},'.'))=[];
dir_img_doublecompr(strcmp({dir_img_doublecompr.name},'..'))=[];
dir_img_doublecompr(strcmp({dir_img_doublecompr.name},'.'))=[];


%quality_matrix = zeros(length(dir_img_orig),2);
quality_matrix = zeros(30,3);

%for i=1:length(dir_img_orig) % iterate over all images
for i=1:30 % for testing... take only the first 30 images
    

%     % original
%     filename = [path_img_orig dir_img_orig(i).name];
%     im = imread(filename);
%     
%     if do_niqe
%         quality_matrix(i,1) = niqe_computequality(im,blocksizerow,blocksizecol,blockrowoverlap,blockcoloverlap,mu_prisparam,cov_prisparam);
%     
%     elseif do_brisque
%         quality_matrix(i,1) = brisque_score(im);
%     
%     elseif do_divine
%         quality_matrix(i,1) = divine(rgb2gray(im));
% 
%     elseif do_nrpqa
%         quality_matrix(i,1)=nrpqa_jpeg_quality_score(rgb2gray(im));
%     end
    

    % single compression
    filename = [path_img_singlecompr dir_img_singlecompr(i).name];
    im = imread(filename);

    if do_niqe
        quality_matrix(i,2) = niqe_computequality(im,blocksizerow,blocksizecol,blockrowoverlap,blockcoloverlap,mu_prisparam,cov_prisparam);
    
    elseif do_brisque
         quality_matrix(i,2) = brisque_score(im);
         
    elseif do_divine
        quality_matrix(i,2) = divine(rgb2gray(im));

    elseif do_nrpqa
        quality_matrix(i,2)=nrpqa_jpeg_quality_score(rgb2gray(im));
    end
    
    
    % double compression
    filename = [path_img_doublecompr dir_img_doublecompr(i).name];
    im = imread(filename);

    if do_niqe
        quality_matrix(i,3) = niqe_computequality(im,blocksizerow,blocksizecol,blockrowoverlap,blockcoloverlap,mu_prisparam,cov_prisparam);
    
    elseif do_brisque
        quality_matrix(i,3) = brisque_score(im);
        
    elseif do_divine
        quality_matrix(i,3) = divine(rgb2gray(im));

    elseif do_nrpqa
        quality_matrix(i,3)=nrpqa_jpeg_quality_score(rgb2gray(im));
    end

end

plot(quality_matrix(:,1),'g');hold on;
plot(quality_matrix(:,2),'r');hold on;
plot(quality_matrix(:,3),'b');
if do_niqe
    title('NR Natural Image Quality Evaluator (NIQE)');
elseif do_brisque
    title('NR Blind/Referenceless Image Spatial Quality Evaluator (BRISQUE)');
elseif do_divine
    title('NR DIIVINE (DIIVINE)');
elseif do_nrpqa
    title('NR Perceptual Quality Assessment of JPEG compressed images');
end
legend('original images (tif)','single compressed','double compressed');
grid on;


%%%
% difference = quality_matrix(:,2)-quality_matrix(:,3);
% plot(difference)
% plot(difference)
% grid on;
% title('NR PQA - Difference between the single and double compressed images');
%%%%
        

    
    
    
    
    